﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace CarbonMeter
{
    public partial class FossilFuelConsumptionForm : Form
    {
        public FossilFuelConsumptionForm()
        {

            InitializeComponent();
        }

        FossilFuelConstant selectedConstant = null;

        private void updateConstants()  //sabit degerlerini textbox ta gosterme
        {
            if (selectedConstant == null) return;

            if (checkBox40.Checked)
            {
                if (((string)comboBox40.SelectedValue).Equals("kWh"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.kwhIn,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("litre"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.litreIn,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("ton"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.tonIn,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("toe"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.toeIn,2);
                }
            }
            else
            {
                if (((string)comboBox40.SelectedValue).Equals("kWh"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.kwhOut,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("litre"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.litreOut,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("ton"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.tonOut,2);
                }
                else if (((string)comboBox40.SelectedValue).Equals("toe"))
                {
                    textBox70.Text = "" + Math.Round((double)selectedConstant.toeOut,2);
                }
            }
            if (((string)comboBox40.SelectedValue).Equals("MWh"))
            {
                textBox70.Text = "" + Math.Round((double)selectedConstant.toeOut, 2);
            }
        }

        private void cleanForm()
        {
            //textBox70.Text = " ";
            textBox50.Text = " ";
            textBox100.Text = " ";
            textBox20.Text = " ";

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
           


            CultureInfo ci = new CultureInfo("tr-TR");
            // System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            DateTimeFormatInfo info = ci.DateTimeFormat;
            dateTimePicker20.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            dateTimePicker20.CustomFormat = info.ShortDatePattern + " " + info.ShortTimePattern;

            comboBox30.DisplayMember = "productName";
            comboBox30.ValueMember = "productId";
            comboBox30.DataSource = Database.getInstance().getProducts(1);// 1--> Fossiul Fueal Group Products
           
            comboBox30.SelectedIndex = 1;
            updateConstants();

            dataGridView20.DataSource = Database.getInstance().getFossilFuelConsumptionByUserRecordAndTime(MainForm.user.userId, DateTime.Now);

            this.BringToFront();
        }

        private void comboBox30_SelectedIndexChanged(object sender, EventArgs e)
        {
            int productId = (int)comboBox30.SelectedValue;
            if (checkBox30.Checked)
            {
                comboBox40.SelectedValue = "kWh";
                comboBox40.Enabled = false;
            }
            else
            {
                comboBox40.DisplayMember = "unitName";
                comboBox40.ValueMember = "unitName";
                comboBox40.DataSource = Database.getInstance().getProductUnits(productId);// 1--> Fossiul Fueal Group Products
               
                comboBox40.SelectedIndex = 0;
                label170.Text = "Karbon / " + (string)comboBox40.SelectedValue + ":";
            }
            selectedConstant = Database.getInstance().getFossilFuelConstant(productId);
            updateConstants();
        }

        private void checkBox30_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox30.Checked)
            {
                comboBox40.SelectedValue = "kWh";
                comboBox40.Enabled = false;
            }
            else
            {
                comboBox40.Enabled = true;
            }
        }

        private void button80_Click(object sender, EventArgs e) //Degiştir Butonu
        {
            DataGridViewRow row = dataGridView20.SelectedRows[0];
            //
            comboBox30.SelectedValue = row.Cells["productId"].Value;
            comboBox40.SelectedValue = row.Cells["unitName"].Value;

            textBox20.Text = row.Cells["amount"].Value.ToString();
            textBox100.Text = "" + Math.Round(Double.Parse(row.Cells["carbonAmount"].Value.ToString()),2);
            button50_Click(sender, e);
            checkBox40.Checked = (bool)row.Cells["inProcess"].Value;
            dateTimePicker20.Value = (DateTime)row.Cells["billDate"].Value;
            if (row.Cells["averageEfficiency"].Value != null)
            {
                checkBox30.Checked = true;
                numericUpDown20.Value = (int)row.Cells["averageEfficiency"].Value;
            }
            else
            {
                checkBox30.Checked = false;
                numericUpDown20.Value = 1;
            }
            //
           // ;

            button60.Text = "GÜNCELLE";
            button60.Enabled = true;
        }

        private void button60_Click(object sender, EventArgs e) //Kaydet Butonu
        {
            try
            {
                if (button60.Text.Equals("KAYDET"))
                    Database.getInstance().addFossilFuelConsumption(selectedConstant.productId, dateTimePicker20.Value,
                   checkBox40.Checked, checkBox30.Checked ? (int)numericUpDown20.Value : -1, (string)comboBox40.SelectedValue,
                     float.Parse(textBox20.Text), float.Parse(textBox100.Text)
                     );
                else
                {
                    int recordId = (int)dataGridView20.SelectedRows[0].Cells["recordId"].Value;
                    Database.getInstance().updateFossilFuelConsumption(recordId, selectedConstant.productId, dateTimePicker20.Value,
                  checkBox40.Checked, checkBox30.Checked ? (int)numericUpDown20.Value : -1, (string)comboBox40.SelectedValue,
                    float.Parse(textBox20.Text), float.Parse(textBox100.Text)
                    );
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                return;
            }

            cleanForm();
            dataGridView20.DataSource = Database.getInstance().getFossilFuelConsumptionByUserRecordAndTime(MainForm.user.userId, DateTime.Now);
            button60.Text = "Kaydet";
        }

        private void button70_Click(object sender, EventArgs e)
        {
            cleanForm();
        }

        private void button50_Click(object sender, EventArgs e)
        {
            try
            {
                float a = float.Parse(textBox20.Text);

                float b = float.Parse(textBox70.Text);
                if (!checkBox30.Checked)
                {
                    textBox100.Text = "" + Math.Round((double)(a * b),2);
                    textBox50.Text = "" + Math.Round((double)(a * b * 44 / 12),2);
                }
                else
                {
                    textBox100.Text = "" + Math.Round((double)(a * b * (float)(100 / numericUpDown20.Value)),2);
                    textBox50.Text = "" + Math.Round((double)((a * b * (float)(100 / numericUpDown20.Value)) * 44 / 12),2);
                }
                button60.Enabled = true;


            }
            catch (Exception)
            {
                MessageBox.Show("Kulllanım miktarını düzgün giriniz.");
            }
        }

        private void comboBox40_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateConstants();
            //if (comboBox30.SelectedValue.GetType() == typeof(string))
            {
                label170.Text = "Karbon / " + (string)comboBox40.SelectedValue + ":";
                label200.Text = "Karbon / " + (string)comboBox40.SelectedValue + ":";
            }
        }

        private void checkBox40_CheckedChanged(object sender, EventArgs e)
        {
            updateConstants();
        }


    }
}
